package com.agent.service.impl;

import com.agent.entity.Dept;
import com.agent.entity.Role;
import com.agent.entity.User;
import com.agent.page.EasyuiPage;
import com.agent.repository.DeptMapper;
import com.agent.repository.RoleMapper;
import com.agent.repository.UserMapper;
import com.agent.service.IDeptService;
import com.agent.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userServiceImpl")
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;
    @Autowired
    IDeptService deptService;
    @Override
    public User findOne(Map<String, String> map) {
        List<User> users = userMapper.findOne(map);
        if(!CollectionUtils.isEmpty(users)){
            User user = users.get(0);
            Role role = roleMapper.findOne(user.getRoleId());
            Map<String,String> paramMap = new HashMap<>();
            paramMap.put("qywxId",user.getDeptId());
            Dept dept = deptService.findOne(paramMap);
            if(dept != null){
                user.setDeptName(dept.getDeptName());
            }
            user.setRoleName((role !=null&& role.getEnabled()) ?role.getRoleName():null);
            return user;
        }
        return null;
    }

    @Override
    public int insert(List<User> users) {
        return userMapper.insert(users);
    }

    @Override
    public int insert(User user) {
        return 0;
    }

    public List<User> list(User user){
        return userMapper.list(user);
    }

    @Override
    public List<User> listByUserName(String userName) {
        User user = new User();
        user.setUserName(userName);
        return userMapper.list(user);
    }

    public List<User> findByRole(){
        return userMapper.findByRole();
    }

    @Override
    public List<User> findUser(User user) {
        List<User> users = userMapper.list(user);
        if(!users.isEmpty()){
            user = users.get(0);
        }
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("qywxId",user.getDeptId());
        Dept dept = deptService.findOne(paramMap);
        if(dept != null){
            user.setDeptName(dept.getDeptName());
        }
        return users;
    }
    @Override
    public List<User> listByAllocation(User user) {
        return userMapper.listByAllocation(user);
    }

    @Override
    public List<User> findByEmail(String email) {
        return userMapper.findLikeByEmail(email);
    }

    @Override
    public int deleteByCreator() {
        return userMapper.deleteByCreator();
    }

    @Override
    public EasyuiPage<User> searchUsers(Integer page, Integer rows, User user) {
        PageHelper.startPage(page, rows,true, true, true);
        List<User> list = userMapper.list(user);
        EasyuiPage<User> pages = new EasyuiPage<User>();
        pages.setRows(list);
        PageInfo<User> pageinfo = new PageInfo<User>(list);
        pages.setTotal(pageinfo.getTotal());
        return pages;
    }

    @Override
    public int delete(String id) {
        return userMapper.delete(id);
    }

    @Override
    public int update(User user) {
        return userMapper.update(user);
    }

    @Override
    public void saveOrUpdate(List<User> users) {
        users.forEach(user ->userMapper.saveOrUpdate(user));
    }


}
